package com.huika.dealontop.utils;

import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.google.android.gms.maps.LocationSource;
import com.huika.dealontop.support.domain.LocationBean;
import com.huika.dealontop.support.domain.LocationBean.MyLocationListener;

/**
 * @author chenyuan
 * @description：按照高德地图定位之后需要销毁，所以不能获取唯一实例文档注：建议在Activity的OnPause()方法中停止定位，并销毁定位资源。
 * @date 2015年11月16日 上午11:25:04
 */
public class GaodeMapUtil implements AMapLocationListener{

	private LocationManagerProxy mLocationManagerProxy;
	private Context ctx;
	private MyLocationListener mLocationListener;
	private LocationSource.OnLocationChangedListener mMapLocationListener;

	public GaodeMapUtil(Context ctx) {
		this.ctx = ctx;
		init();
	}

	public void setmLocationListener(MyLocationListener mLocationListener) {
		this.mLocationListener = mLocationListener;
	}
	
	/**
	 * 初始化定位
	 */
	private void init(){
		mLocationManagerProxy = LocationManagerProxy.getInstance(ctx);
	}
	
	/**
	 * 开始定位
	 * 
	 */
	public void start(){
		if(mLocationManagerProxy == null){
			init();
		}
		// 此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
		// 注意设置合适的定位时间的间隔（最小间隔支持为2000ms），并且在合适时间调用removeUpdates()方法来取消定位请求
		// 在定位结束后，在合适的生命周期调用destroy()方法
		// 其中如果间隔时间为-1，则定位只定一次,
		// 在单次定位情况下，定位无论成功与否，都无需调用removeUpdates()方法移除请求，定位sdk内部会移除
		mLocationManagerProxy.requestLocationData(
				LocationProviderProxy.AMapNetwork, 120 * 1000, 10, this);
	}
	
	/**
	 * 结束定位
	 * 
	 */
	public void stop(){
		if(mLocationManagerProxy == null)
			return;
		// 移除定位请求
		mLocationManagerProxy.removeUpdates(this);
		// 销毁定位
		mLocationManagerProxy.destroy();
		mLocationManagerProxy = null;
	}

	@Override
	public void onLocationChanged(Location location) {}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {}

	@Override
	public void onProviderEnabled(String provider) {}

	@Override
	public void onProviderDisabled(String provider) {}

	public void setMapLocationListener(LocationSource.OnLocationChangedListener listener) {
		this.mMapLocationListener = listener;
	}
	
	@Override
	public void onLocationChanged(AMapLocation amapLocation) {
		LocationBean locationBean = null;
		int locType = -1;
		if (amapLocation != null
				&& amapLocation.getAMapException().getErrorCode() == 0) {
			if(mMapLocationListener != null){
				mMapLocationListener.onLocationChanged(amapLocation);
			}
			// 定位成功回调信息，设置相关消息
			locationBean = LocationBean.saveCurrentLocation(amapLocation.getLatitude(), 
					amapLocation.getLongitude(), 
					amapLocation.getProvince()==null ? "" : amapLocation.getProvince(), 
					amapLocation.getCity(), 
					amapLocation.getDistrict(), 
					0);
			locationBean.road = amapLocation.getRoad();
			locationBean.poi = amapLocation.getPoiName();
		}else{
//			DebugTool.e("AmapErr","Location ERR:" + amapLocation.getAMapException().getErrorCode());
			Log.e("AmapErr","Location ERR:" + amapLocation.getAMapException().getErrorCode());
			if(amapLocation != null){
				locType = amapLocation.getAMapException().getErrorCode();
			}
			locationBean = new LocationBean(locType);
		}
		
		if(mLocationListener != null){
			mLocationListener.onLocation(locationBean);
		}
	}
}
